package com.yenlien.traceyun.control.controller;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.control.entity.Accredit;
import com.yenlien.traceyun.control.interceptor.UserAuthUtil;
import com.yenlien.traceyun.control.service.AccreditService;
import com.yenlien.traceyun.webase.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/accredit")
public class AccreditController extends BaseController {

    @Autowired
    private AccreditService accreditService;

    /**
     * 新增授权管理
     *
     * @param accredit
     * @param request
     * @return
     */
    @PostMapping("/save")
    public Object save(Accredit accredit, HttpServletRequest request) {
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("file"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("file");
        }
        accreditService.save(accredit, files);
        return success("success");
    }

    @PostMapping("/save2")
    public Object save2(@RequestBody Accredit accredit) {
        accreditService.save2(accredit);
        return success("success");
    }

    /**
     * 页面显示
     *
     * @param params
     * @return
     */
    @PostMapping("/inquire")
    public Object inquire(@RequestBody JSONObject params) {
        Integer page = params.getInteger("page");
        if (page > 0) {
            Integer page1 = page - 1;
            return buildPage(accreditService.select(page1, UserAuthUtil.getLoginUser().getCompanyId()));
        } else {
            return fail("页面加载错误");
        }
    }

    /**
     * 搜索栏(模糊查询)
     *
     * @param params
     * @return
     */
    @PostMapping("/search")
    public Object search(@RequestBody JSONObject params) {
        String accreditName = params.getString("accreditName");
        String accreditNames = "%" + accreditName + "%";
        if (!StringUtils.isEmpty(accreditName)) {
            return success(accreditService.search(accreditNames));
        } else {
            return fail("信息未存在");
        }
    }

    /**
     * 根据id查询
     *
     * @param params
     * @return
     */
    @PostMapping("/selectId")
    public Object selectId(@RequestBody JSONObject params) {
        String id = params.getString("id");
        if (id.length() > 0) {
            return success(accreditService.selectId(id));
        } else {
            return fail("授权不存在");
        }
    }

    /**
     * 根据id修改
     *
     * @param
     */
    @PostMapping("/update")
    public Object update(Accredit accredit, HttpServletRequest request) {
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("file"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("file");
        }
        accreditService.update(accredit, files);
        return success("success");
    }


}
